package com.iasii.app.citylist.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.iasii.app.citylist.R;
import com.iasii.app.citylist.entity.City;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eenie on 2016/12/12 at 14:02
 * Email: 472279981@qq.com
 * Des:
 */

public class SearchAdapter extends BaseAdapter implements Filterable {
    ArrayFilter mFilter;
    List<City> citiesData = new ArrayList<>();
    private final Object mLock = new Object();
    private int maxMatch = 10;//最多显示多少个选项,负数表示全部
    private Context mContext;
    List<City> searchRes = new ArrayList<>();
    public SearchAdapter( Context context) {
        this.mContext = context;
    }

    public void setData(List<City> citiesData) {
        this.citiesData.addAll(citiesData);
    }

    @Override
    public int getCount() {
        return searchRes.size();
    }

    @Override
    public Object getItem(int position) {
        return searchRes.get(position).getName();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tvCity;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_search_city_res, null);
            tvCity = (TextView) convertView.findViewById(R.id.tv_city_name);
            convertView.setTag(tvCity);
        } else {
            tvCity = (TextView) convertView.getTag();
        }


        tvCity.setText(searchRes.get(position).getName());
        return convertView;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    public int getRawPos(int position) {
        return citiesData.indexOf(searchRes.get(position));
    }


    private class ArrayFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

//          if (mOriginalValues == null) {
//                synchronized (mLock) {
//                    mOriginalValues = new ArrayList<String>(mObjects);//
//                }
//            }



            if (constraint == null || constraint.length() == 0) {



                synchronized (mLock) {
                    Log.i("tag", "mOriginalValues.size=" + citiesData.size());
                    ArrayList<City> list = new ArrayList<City>(citiesData);
                    results.values = list;
                    results.count = list.size();
                    return results;
                }
            } else {

                Log.e("filter", "keyword = " + constraint.toString());


                String prefixString = constraint.toString().toLowerCase();

                final int count = citiesData.size();
                Log.e("filter", "size = " + count);
                final ArrayList<City> newValues = new ArrayList<City>(count);

                for (int i = 0; i < count; i++) {

                    City city = citiesData.get(i);

                    final String value =city.getName();
                    final String valueText = value.toLowerCase();
//                    if(valueText.contains(prefixString)){//匹配所有
//
//                    }
                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {  //源码 ,匹配开头
                        newValues.add(city);
                    }
//                    else {
//                        final String[] words = valueText.split(" ");//分隔符匹配，效率低
//                        final int wordCount = words.length;
//
//                        for (int k = 0; k < wordCount; k++) {
//                            if (words[k].startsWith(prefixString)) {
//                                newValues.add(value);
//                                break;
//                            }
//                        }
//                    }
                    if (maxMatch > 0) {//有数量限制
                        if (newValues.size() > maxMatch - 1) {//不要太多
                            break;
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            searchRes.clear();
            searchRes.addAll((List<City>) results.values);
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

}

